package com.ssy.controller;

import cn.hutool.core.util.StrUtil;
import com.ssy.common.CodeMsg;
import com.ssy.common.Result;
import com.ssy.entity.User;
import com.ssy.service.UserService;
import com.ssy.service.impl.UserServiceImpl;
import com.ssy.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * UserServlet
 *
 * @author ssy
 * @date 2020/11/14
 **/
@WebServlet(urlPatterns = {"/user.do"})
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = -8112137359226184120L;

    UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("login".equals(type)) {
            login(req, resp);
        } else if ("exit".equals(type)) {
            exit(req, resp);
        } else if ("userList".equals(type)) {
            userList(req, resp);
        } else if ("addUser".equals(type)) {
            addUser(req, resp);
        } else if ("delUser".equals(type)) {
            delUser(req, resp);
        } else if ("delOneUser".equals(type)) {
            delOneUser(req, resp);
        } else if ("rest".equals(type)) {
            rest(req, resp);
        } else if ("updateUser".equals(type)) {
            updateUser(req, resp);
        } else if ("updatePwd".equals(type)) {
            updatePwd(req, resp);
        }
    }

    /**
     * 修改用户密码
     *
     * @param req
     * @param resp
     */
    private void updatePwd(HttpServletRequest req, HttpServletResponse resp) {
        String password = req.getParameter("password");
        String newPassword = req.getParameter("newPassword1");
        User user = (User) req.getSession().getAttribute("user");
        Result rs = this.userService.updatePwd(password, newPassword, user.getuId());
        RespUtil.write(resp, rs);
    }

    /**
     * 修改用户信息
     *
     * @param req
     * @param resp
     */
    private void updateUser(HttpServletRequest req, HttpServletResponse resp) {
        String uname = req.getParameter("uname");
        int uId = Integer.parseInt(req.getParameter("uId"));
        int utype = Integer.parseInt(req.getParameter("utype"));
        Result rs = this.userService.updateUser(uname, uId, utype);
        RespUtil.write(resp, rs);
    }

    /**
     * 重置密码
     *
     * @param req
     * @param resp
     */
    private void rest(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Result rs = this.userService.restUserPassword(id);
        RespUtil.write(resp, rs);
    }

    /**
     * 删除单个用户
     *
     * @param req
     * @param resp
     */
    private void delOneUser(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Result rs = this.userService.deleteOneUser(id);
        RespUtil.write(resp, rs);
    }

    /**
     * 批量删除用户
     *
     * @param req
     * @param resp
     */
    private void delUser(HttpServletRequest req, HttpServletResponse resp) {
        String[] ids = req.getParameterValues("id");
        Result result = userService.deteleUser(ids);
        RespUtil.write(resp, result);
    }

    /**
     * 添加用户
     *
     * @param req
     * @param resp
     */
    private void addUser(HttpServletRequest req, HttpServletResponse resp) {
        String uname = req.getParameter("uname");
        int utype = Integer.parseInt(req.getParameter("utype"));
        Result rs = userService.addUser(uname, utype);
        RespUtil.write(resp, rs);
    }

    /**
     * 用户登录
     *
     * @param req
     * @param resp
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) {
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String checkCode = req.getParameter("checkCode");
        String code = (String) req.getSession().getAttribute("code");
        if (!StrUtil.equalsIgnoreCase(code, checkCode)) {
            Result rs = new Result(CodeMsg.USER_CHECK_CODE_ERROR);
            RespUtil.write(resp, rs);
            return;
        }
        Result rs = this.userService.login(loginName, password);
        if (rs.getCode() == CodeMsg.SUCCES.getCode()) {
            req.getSession().setAttribute("user", rs.getData());
        }
        RespUtil.write(resp, rs);
    }

    /**
     * 用户退出
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void exit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.getSession().removeAttribute("user");
        resp.sendRedirect("index.jsp");
    }

    /**
     * 用户列表
     *
     * @param req
     * @param resp
     */
    private void userList(HttpServletRequest req, HttpServletResponse resp) {
        String uname = req.getParameter("uname");
        String upwd = req.getParameter("upwd");
        String utype = req.getParameter("utype");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        HashMap<String, Object> param = new HashMap<>();
        if (StrUtil.isNotBlank(uname)) {
            param.put("uname", uname);
        }
        if (StrUtil.isNotBlank(upwd)) {
            param.put("upwd", upwd);
        }
        if (StrUtil.isNotBlank(utype)) {
            param.put("utype", utype);
        }
        if (StrUtil.isNotBlank(page)) {
            param.put("page", page);
        }
        if (StrUtil.isNotBlank(limit)) {
            param.put("limit", limit);
        }
        Result result = userService.queryPage(param, page, limit);
        RespUtil.write(resp, result);
    }


}
